﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cat;
using System.Data.Odbc;
using System.Security.Cryptography;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmproduct : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcConnection con = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbcon = new DatabaseConnection();
        public frmproduct()
        {
            InitializeComponent();
            btnNew.Click += new EventHandler(btnNew_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            this.Load += new EventHandler(frmproduct_Load);
            this.FormClosing += new FormClosingEventHandler(frmproduct_FormClosing);
            cmbCategory.SelectedIndexChanged += new EventHandler(cmbCategory_SelectedIndexChanged);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            txtMarkUp.LostFocus += new EventHandler(txtMarkUp_LostFocus);
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
        
            
        }
        void txtMarkUp_LostFocus(object sender, EventArgs e)
        {
            comSellingprice();   
        }
        void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                con = new OdbcConnection(cs.DBConn);

                con.Open();
                cmd = con.CreateCommand();

                cmd.CommandText = "SELECT * from tblcategory WHERE CategoryName = '" + cmbCategory.Text + "'";
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    textBox2.Text = rdr.GetInt32(0).ToString().Trim();
                }
                if ((rdr != null))
                {
                    rdr.Close();
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
                
                cmbsubCategory.Items.Clear();
                cmbsubCategory.Text = "";
                cmbsubCategory.Enabled = true;
                cmbsubCategory.Focus();

                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select distinct RTRIM(SubCName) from tblCategory,tblsubCategory where tblcategory.CategoryID=tblsubcategory.CategoryID and CategoryName= '" + cmbCategory.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    cmbsubCategory.Items.Add(rdr[0]);
                }
                con.Close();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void frmproduct_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            frmMain frmM = new frmMain();
            frmM.Show();
        }
        void frmproduct_Load(object sender, EventArgs e)
        {
            FillCombo();
            Fillsupplier();
            FillUnit();
            autoid();
          
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            if (txtProductName.Text == "")
            {
                MessageBox.Show("Please enter product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProductName.Focus();
                return;
            }
            if (cmbCategory.Text == "")
            {
                MessageBox.Show("Please select category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbCategory.Focus();
                return;
            }
            if (cmbsubCategory.Text == "")
            {
                MessageBox.Show("Please select sub category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbsubCategory.Focus();
                return;
            }
          
            if (cmbUnit.Text == "")
            {
                MessageBox.Show("Please Select unit ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
                txtSupplierPrice.Focus();
                return;
            }
            if (txtUnitQTY.Text == "")
            {
                MessageBox.Show("Please enter supplier price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
                txtUnitQTY.Focus();
                return;
            }
            if (txtSupplierPrice.Text == "")
            {
                MessageBox.Show("Please enter supplier price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
                txtSupplierPrice.Focus();
                return;
            }
            if (txtMarkUp.Text == "")
            {
                MessageBox.Show("Please enter Mark up Value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
                txtSupplierPrice.Focus();
                return;
            }
            if (txtQty.Text == "")
            {
                MessageBox.Show("Please enter Product Quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
                txtQty.Focus();
                return;
            }
            if (cmbSupplier.Text == "")
            {
                MessageBox.Show("Please select supplier", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
                txtSupplierPrice.Focus();
                cmbSupplier.Focus();
                return;
            }
            try
            {
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select ProductName from tblproduct where ProductName='" + txtProductName.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Product Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProductName.Text = "";
                    txtProductName.Focus();

                    if ((rdr != null))
                    {
                        rdr.Close();
                    }
                    return;
                }
                autoid();
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string cb = "Insert into tblproduct(ProductID,Productname,CategoryName,SubCname,SupplierName) VALUES ('" + txtProductID.Text + "','" + txtProductName.Text + "','" + cmbCategory.Text + "','" + cmbsubCategory.Text + "','"+cmbSupplier.Text+"')";
                cmd = new OdbcCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                string pr ="Insert into tblpricelist(ProductID,Unit,UnitQTY,SupplierPrice,SellingPrice,MarkUp)Values('"+txtProductID.Text+"','"+cmbUnit.Text+"','"+txtUnitQTY.Text+"','"+txtSupplierPrice.Text+"','"+txtSellingPrice.Text+"','"+txtMarkUp.Text+"')";
                cmd = new OdbcCommand(pr);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnSave.Enabled = false;
               
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        void btnNew_Click(object sender, EventArgs e)
        {
            reset();
            autoid();
        }
        void autoid()
        {
            txtProductID.Text = "PID-" + GetUniqueKey(5);

        }
        void reset()
        {
            txtProductName.Text = "";
            txtQty.Text = "";
            txtSellingPrice.Text = "";
            txtSupplierPrice.Text = "";
            txtMarkUp.Text = "";
            txtUnitQTY.Text = "";
            cmbCategory.Text = null;
            cmbsubCategory.Text = null;
            cmbUnit.Text = null;
            cmbSupplier.Text = null;
            
            
          
        }
        public void FillCombo()
        {
            try
            {

                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "select RTRIM(CategoryName) from tblcategory order by CategoryName";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    cmbCategory.Items.Add(rdr[0]);
                    
                }
                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62];
            chars = "123456789".ToCharArray();
            byte[] data = new byte[1];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }
        public void Fillsupplier()
        {
            try
            {

                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select RTRIM(SupplierName) from tblsupplier order by SupplierName";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    
                    cmbSupplier.Items.Add(rdr[0]);

                }
                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void FillUnit()
        {
            try
            {
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select RTRIM (UnitName) from tblunit order by UnitName";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {

                    cmbUnit.Items.Add(rdr[0]);


                }
                con.Close();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        
        
        }
        void comSellingprice()
        {
            string s = txtSupplierPrice.Text;
            string m = txtMarkUp.Text;
            string result;
            int supp = int.Parse(s);
            int mark = int.Parse(m);
            int sp;
            sp = supp + mark;
            result = sp.ToString();
            txtSellingPrice.Text = "" + sp;
        }

        
        
    }

}
